package com.fafu.rbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fafu.common.web.bean.PageBean;
import com.fafu.common.web.bean.Result;
import com.fafu.common.web.bean.ResultUtil;
import com.fafu.common.web.controller.BaseController;
import com.fafu.common.web.mybatis.QueryWrapperUtil;
import com.fafu.rbac.conterter.UserConverter;
import com.fafu.rbac.domain.entity.User;
import com.fafu.rbac.domain.query.UserQuery;
import com.fafu.rbac.domain.vo.UserVO;
import com.fafu.rbac.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (tb_member)表控制层
 *
 * @author hc
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<User> {

    @Resource
    private UserService userService;

    public UserController(IService<User> service) {
        super(service);
    }

    @PostMapping("/list")
    public Result list(@RequestBody UserQuery userQuery) {
        IPage<User> page = new Page<>(userQuery.getPageNum(), userQuery.getPageSize());

        //将查询对象userQuery转换成QueryWrapper对象
        QueryWrapper<User> queryWrapper = QueryWrapperUtil.build(userQuery);

        userService.page(page, queryWrapper);

        PageBean<User> pageBean = PageBean.init(page);
        PageBean<UserVO> userVOPageBean = new UserConverter().entityPageBean2VOPageBean(pageBean);

        return ResultUtil.success().addData("pageBean", userVOPageBean);
    }

    @GetMapping("/fun1")
    @PreAuthorize("hasAuthority('sys:user:view')")
    public Result fun1(){
        return ResultUtil.success("fun111 sys:user:view");
    }

    @GetMapping("/fun2")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result fun2(){
        return ResultUtil.success("fun1222 sys:user:update");
    }

    @GetMapping("/fun3")
    @PreAuthorize("hasRole('admin')")
    public Result fun3(){
        return ResultUtil.success("fun3333 admin");
    }

    @GetMapping("/fun4")
    @PreAuthorize("hasRole('vip')")
    public Result fun4(){
        return ResultUtil.success("fun444 vip");
    }
}
